# 购物小票信息
order_id = "SP2025101500789"
time = "2025-10-15 18:25:30"
items = [
    {"name": "阿克苏苹果", "quantity": 2, "unit_price": 8.99, "amount": 17.98},
    {"name": "香蕉", "quantity": 1, "unit_price": 5.50, "amount": 5.50},
    {"name": "纯牛奶", "quantity": 1, "unit_price": 65.80, "amount": 65.80},
    {"name": "全麦面包", "quantity": 1, "unit_price": 12.50, "amount": 12.50},
    {"name": "薯片", "quantity": 3, "unit_price": 4.50, "amount": 13.50},
    {"name": "矿泉水", "quantity": 12, "unit_price": 1.50, "amount": 18.00}
]
total_count = len(items)
total_amount = sum(item["amount"] for item in items)
discount = 5.00
discounted_amount = total_amount - discount
actual_payment = 130.00
change = actual_payment - discounted_amount
cashier = "收银员012"
store_name = "鲜丰超市"
phone = "400-123-4567"

# 定义小票宽度
receipt_width = 48

# 生成购物小票
receipt = "=" * receipt_width + "\n"
receipt += f"{store_name:^{receipt_width}}\n"  # 居中显示超市名称
receipt += f"{'购物小票':^{receipt_width}}\n"   # 居中显示小票标题
receipt += "=" * receipt_width + "\n"

# 订单信息左对齐，时间右对齐
receipt += f"单号: {order_id:<{receipt_width - 5 - len(time)}}{time}\n"
receipt += "\n"

# 商品列表标题行
receipt += f"{'商品名称':<18}{'数量':>6}{'单价':>10}{'金额':>14}\n"
receipt += "-" * receipt_width + "\n"

# 商品列表内容
for item in items:
    # 确保中文和英文混合时对齐
    name = item['name']
    # 计算名称占用的宽度（中文算2字符，英文算1字符）
    name_width = sum(2 if '\u4e00' <= c <= '\u9fff' else 1 for c in name)
    # 补充空格使名称部分对齐
    spaces_needed = 18 - name_width
    receipt += f"{name}{' ' * spaces_needed}{item['quantity']:>6d}{item['unit_price']:>10.2f}{item['amount']:>14.2f}\n"

receipt += "-" * receipt_width + "\n"

# 金额信息右对齐
receipt += f"{'商品总数:':>{receipt_width - 6}} {total_count} 件\n"
receipt += f"{'应付金额:':>{receipt_width - 10}} {total_amount:.2f} 元\n"
receipt += f"{'优惠金额:':>{receipt_width - 10}} {discount:.2f} 元\n"
receipt += f"{'实付金额:':>{receipt_width - 10}} {actual_payment:.2f} 元\n"
receipt += f"{'找零金额:':>{receipt_width - 10}} {change:.2f} 元\n"
receipt += "\n"

# 收银员信息左对齐
receipt += f"收银员: {cashier}\n"
receipt += "=" * receipt_width + "\n"

# 底部信息居中
receipt += f"{'感谢光临，欢迎下次再来！':^{receipt_width}}\n"
receipt += f"{'客服电话：' + phone:^{receipt_width}}\n"
receipt += "=" * receipt_width + "\n"

print(receipt)
